package com.note.util;

import com.note.common.utils.StringUtils;
import com.note.common.utils.spring.SpringUtils;
import com.note.config.SftpProperties;
import org.apache.commons.collections4.CollectionUtils;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 文件nginx代理前缀设置
 */
public class FileProxyUtil {


    /**
     * 删除url的nginx这些代理地址
     * @param url
     * @return
     */
    public static String removeProxyPerfix(String url){
       if(StringUtils.isEmpty(url)){
           return null;
       }
        SftpProperties sftpProperties = SpringUtils.getBean(SftpProperties.class);
        int proxyIdx = url.indexOf(sftpProperties.getProxyHost());
        if(proxyIdx < 0){
            return url;
        }
        return url.substring(sftpProperties.getProxyHost().length()+1);


    }


    public static <T> List<T> setProxyPrefix(List<T> list, Function<T, String> function, BiFunction<T, String, T> setFuncion){
        if(CollectionUtils.isNotEmpty(list)){
            SftpProperties sftpProperties = SpringUtils.getBean(SftpProperties.class);
            list.forEach(x -> {
                String previewUrl = function.apply(x);
                if(StringUtils.isNotEmpty(previewUrl)){
                    previewUrl = sftpProperties.getProxyHost()+ File.separator+previewUrl;
                    setFuncion.apply(x, previewUrl);
                }
            });
        }
        return list;
    }


    public static <T> T setProxyPrefix(T ob, Function<T, String> function, BiFunction<T, String, T> setFuncion){
        if(ob == null){
            return null;
        }
        T rst = setProxyPrefix(Stream.of(ob).collect(Collectors.toList()), function, setFuncion).get(0);
        return rst;
    }
}
